package com.cgbl.cg.ui.resource

import android.graphics.Typeface
import android.os.Bundle
import android.util.TypedValue
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentStatePagerAdapter
import androidx.viewpager.widget.ViewPager
import com.alibaba.android.arouter.facade.annotation.Route
import com.cgbl.cg.BR
import com.cgbl.cg.R
import com.cgbl.cg.databinding.AppActivityResourceMenuBinding
import com.cgbl.cg.view.SelectCoursePop
import com.cgbl.cg.vm.resource.ResourceMenuActViewModel
import com.lxj.xpopup.XPopup
import com.robot.base.base.activity.GLBaseActivity
import com.robot.base.configs.AppConstants
import com.robot.base.model.CourseAllTypeBean
import com.robot.base.model.FilterBean
import com.robot.base.util.DisplayHelper

@Route(path = AppConstants.Router.RESOURCE.A_RESOURCE_MENU)
class ResourceMenuActivity :
    GLBaseActivity<AppActivityResourceMenuBinding, ResourceMenuActViewModel>() {
    var filterBean = FilterBean()
    private var typeTab = 0

    private var mTitles = mutableListOf<String>()

    private var fragmentList = mutableListOf<Fragment>()
    override fun initContentView(savedInstanceState: Bundle?): Int =
        R.layout.app_activity_resource_menu

    override fun initVariableId(): Int = BR.viewModel

    override fun initComponents() {

    }

    override fun initParam() {
        super.initParam()
        val bean = intent.getSerializableExtra(AppConstants.BundleKey.FILTER_RESOURCE_BEAN)
        if (bean != null) {
            filterBean = bean as FilterBean
        }
    }

    fun initViewPager(it: CourseAllTypeBean) {
        mTitles.clear()
        fragmentList.clear()
        if (it.childrenList.isNullOrEmpty()) {
            return
        }
        it.childrenList?.forEach {
            mTitles.add(it.name ?: "")
            fragmentList.add(ResourceMenuFragment.newInstance(it.id ?: "", filterBean))
        }

        binding.mViewPager2.adapter = object : FragmentStatePagerAdapter(supportFragmentManager) {
            override fun getItem(position: Int): Fragment = fragmentList[position]
            override fun getCount(): Int = fragmentList.size
        }
        binding.mViewPager2.offscreenPageLimit = fragmentList.size
        binding.tabLayout.setViewPager(binding.mViewPager2, mTitles.toTypedArray())
        binding.tabLayout.currentTab = typeTab
        setTabSize(typeTab)
        binding.mViewPager2.setOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {

            }

            override fun onPageScrolled(
                position: Int,
                positionOffset: Float,
                positionOffsetPixels: Int
            ) {

            }

            override fun onPageSelected(position: Int) {
                typeTab = position
                setTabSize(position)
            }

        })
    }

    override fun initData() {
        super.initData()
        viewModel.allType()

    }

    //select tab textsize
    private fun setTabSize(position: Int) {
        for (i in mTitles.indices) {
            if (binding.tabLayout.getTitleView(i) != null) {
                if (position == i) {
                    binding.tabLayout.getTitleView(i).setTextSize(
                        TypedValue.COMPLEX_UNIT_PX,
                        DisplayHelper.dpToPx(16).toFloat()
                    )
                } else {
                    binding.tabLayout.getTitleView(i).setTextSize(
                        TypedValue.COMPLEX_UNIT_PX,
                        DisplayHelper.dpToPx(14).toFloat()
                    )
                }
                binding.tabLayout.getTitleView(i).typeface = Typeface.DEFAULT_BOLD
            }
        }
    }

    override fun initViewObservable() {
        super.initViewObservable()
        viewModel.uc.selectType.observe(this) {
            val selectCoursePop =
                SelectCoursePop(this, viewModel.typeName.get() ?: "", viewModel.list)
            XPopup.Builder(this)
                .atView(binding.clTitle)
                .asCustom(selectCoursePop)
                .show()

            selectCoursePop.setConfirmClickListener {
                selectCoursePop.dismiss()
                viewModel.setTabData(it)
            }
        }

        viewModel.uc.tabDateEvent.observe(this) {
            initViewPager(it)
        }

    }
}